package com.wlps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlps.contant.Contant;
import com.wlps.entity.OrderRecord;
import com.wlps.entity.SysUser;
import com.wlps.mapper.SysUserMapper;
import com.wlps.result.ResultPageVo;
import com.wlps.result.ResultVo;
import com.wlps.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlps.vo.OperatorDetailInfoVo;
import com.wlps.vo.SenderRoleUserVo;
import com.wlps.vo.UserParm;
import com.wlps.vo.sendcenter.warehouse.InOutVo;
import com.wlps.vo.sendcenter.warehouse.WarehouseSettingVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 员工表(sys_user) 服务实现类
 * </p>
 *
 * @author ZWYZY
 * @since 2020-07-30
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService, InitializingBean {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    SysUserMapper sysUserMapper;
    @Override
    //根据用户姓名查询用户信息
    public SysUser getUserByUserName(String username) {
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        query.lambda().eq(SysUser::getUsername, username);
        return this.baseMapper.selectOne(query);
    }

    @Override
    public ResultPageVo<List<SysUser>> selectUser(UserParm userParm) {

        initSysUser();

        //总条数
        int count;
        //偏移量
        int fromNum;
        //终止量
        int toNum;
        List<SysUser> sysUserList = (List<SysUser>) redisTemplate.opsForValue().get(Contant.USERS_KEY);


        if (StringUtils.isBlank(userParm.getLoginName()) && StringUtils.isBlank(userParm.getMobile())) {
            //不是条件查询，redis
            count = sysUserList.size();

        } else {
            Stream<SysUser> stream = sysUserList.stream();
            if (!StringUtils.isBlank(userParm.getLoginName())) {
                String name = userParm.getLoginName();
                stream = stream.filter(x -> x != null && name.equals(x.getLoginName()));
            }
            if (!StringUtils.isBlank(userParm.getMobile())) {
                String name = userParm.getMobile();
                stream = stream.filter(x -> x != null && name.equals(x.getMobile()));
            }
//
            sysUserList = stream.collect(Collectors.toList());
            count = sysUserList.size();
        }
        //当前页从第几条开始查
        fromNum = userParm.getPageSize() * (userParm.getCurrentPage() - 1);
        if (fromNum >= count) {
//            sysUserList=null;
        } else {
            //当前页最后第几条
            toNum = fromNum + userParm.getPageSize() >= count ? count : fromNum + userParm.getPageSize();
            sysUserList = sysUserList.subList(fromNum, toNum);
        }
        //分页返回值封装
        ResultPageVo resultPageVo = new ResultPageVo("响应成功", 200, (long) userParm.getPageSize(), (long) userParm.getCurrentPage(), (long) count, sysUserList);


        return resultPageVo;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        initSysUser();
    }
    @Override
    public List<OperatorDetailInfoVo> operatorDetailinfoById(long id){
       return  sysUserMapper.operatorDetailinfoById(id);
    }

    @Override
    public List<String> queryCommodityByOrderId(long id) {
        return sysUserMapper.queryCommodityByOrderId(id);
    }

    @Override
    public List<OperatorDetailInfoVo> operatorDetailAll() {
        return sysUserMapper.operatorDetailAll();
    }

    @Override
    public ResultVo<List<SenderRoleUserVo>> getSenderRoleUserVo() {
        List<SenderRoleUserVo> senderRoleUserVos=baseMapper.getSenderRoleVo();
        return new ResultVo<>("获取配送员列表成功",200,senderRoleUserVos);
    }

    /**
     * 初始化缓存
     */
    void initSysUser() {
        if (!redisTemplate.hasKey(Contant.USERS_KEY)) {
            List<SysUser> sysUserList = baseMapper.selectUsers();
            redisTemplate.opsForValue().set(Contant.USERS_KEY, sysUserList);
            log.info("初始化所有用户-------存入缓存数据");
        }
    }
}
